//
//  Concept_DispatchBarrier_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/10/22.
//

/**
 ## 参考链接
 - [19-探究iOS底层原理|多线程技术【GCD源码分析2:栅栏函数dispatch_barrier_(a)sync、信号量dispatch_semaphore】](https://juejin.cn/post/7116897833126625316?searchId=202310222010572623358D83956CC00B2A)
 
 */

import UIKit

class Concept_DispatchBarrier_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        test1()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("\(#function)")
    }
    

}

extension Concept_DispatchBarrier_DemoVC {
    func test1() {
        let concurrentQueue = DispatchQueue(label: "ap.swift.concurrent.test1.queue", attributes: .concurrent)
        print("\(#function), LOG 1")
        concurrentQueue.async {
            print("\(#function), 任务11111")
        }
        concurrentQueue.async {
            print("\(#function), 任务22222")
        }
        concurrentQueue.async(flags: .barrier) {
            sleep(10)
            print("\(#function), async（异步）barrier（栅栏）, 任务33333")
        }
        concurrentQueue.async {
            print("\(#function), 任务44444")
        }
        concurrentQueue.async {
            print("\(#function), 任务55555")
        }
        print("\(#function), LOG 2")
    }
    
    /**
     test1(), LOG 1
     test1(), LOG 2
     test1(), 任务11111
     test1(), 任务22222
     test1(), async（异步）barrier（栅栏）, 任务33333
     test1(), 任务44444
     test1(), 任务55555
     
     */
}
